//引入请求函数
import { reqCategoryList, reqGetBannerList, reqGetFloorList } from "@/api";


//home模块小仓库
//state:仓库存储数据的地方
const state = {
    //state中的数据默认初始值要根据服务器返回的数据来设置，不能自己随便设置一个值类型
    //根据接口返回值初始化
    categoryList: [],
    bannerList: [],
    floorList: []
};

//mutations:修改仓库数据的方法
const mutations = {
    CATEGORYLIST(state, categoryList) {
        state.categoryList = categoryList
    },
    BANNERLIST(state, bannerList) {
        state.bannerList = bannerList
    },
    FLOORLIST(state, floorList) {
        state.floorList = floorList
    }
};

//actions:处理action,派发数据请求,处理业务逻辑,也可以处理异步
const actions = {
    //通过API里面的接口函数调用,发送ajax请求,获取服务器数据
    async categoryList(context) {
        let result = await reqCategoryList();
        // console.log(result);
        // console.log(result.data);
        result.data.pop();
        if (result.code == 200) {
            context.commit('CATEGORYLIST', result.data);
        }
    },
    //获取首页轮播图的数据
    async getBannerList(context) {
        let result = await reqGetBannerList();
        // console.log(result.data);
        if (result.code == 200) {
            context.commit('BANNERLIST', result.data);
        }
    },
    //获取首页floor组件的数据
    async getFloorList(context) {
        let result = await reqGetFloorList();
        // console.log(result.data);
        if (result.code == 200) {
            context.commit('FLOORLIST', result.data);
        }
    }
};


//类似于计算属性,简化仓库数据
const getters = {};

//对外暴露小仓库
export default {
    state,
    actions,
    mutations,
    getters
}